<template>
    <el-dialog :visible="visible" title="补录" width="400px" @close="onClose" @open="onOpen">
        <el-form ref="form" :model="form" label-width="120px">
            <el-form-item label="泵机车牌/编号：">
                <el-select v-model="form.pumpNo" placeholder="请选择" :disabled="config.pumpWay !== 1">
                    <el-option
                        v-for="item in pumpNoOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="泵工：">
                <el-select v-model="form.pumpManId" placeholder="请选择" :disabled="config.pumpWay !== 1" @change="selectPumpMan">
                    <el-option
                        v-for="item in pumpManOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button class="bottom_all_two" @click="onClose">取 消</el-button>
            <el-button class="bottom_all" type="primary" @click="onConfirm">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { getPumpNoList, updateApplyDet } from "@/api/smart/schedul/pump.js";
    import { useruser_list } from "@/api/smart/schedul/f.js";

    export default {
        data() {
            return {
                pumpNoOptions: [], // 泵机车牌/编号下拉数据
                pumpManOptions: [], // 泵工下拉数据
                form: {
                    pumpNo: '',
                    pumpManId: '',
                    pumpManName: '',
                }
            };
        },
        props: {
            config: {
                type: Object,
                default: () => ({}),
            },
            visible: {
                type: Boolean,
            },
        },
        methods: {
            selectPumpMan(e) { // 选择泵工
                for(let i = 0; i < this.pumpNoOptions.length; i++) {
                    if(this.pumpNoOptions[i].value === e) {
                        this.form.pumpManName = this.pumpNoOptions[i].label
                    }
                }
            },
            async onOpen() {
                this.form = {
                    pumpNo: this.config.pumpNo,
                    pumpManId: this.config.pumpManId,
                    pumpManName: this.config.pumpManName,
                }
                const getPumpNoListParams = {
                    proId: this.config.logisticsNo,
                    pumpTypeId: this.config.pumpTypeId
                };
                const getUserListParams = {
                    roleId: "1687e52bdb9a44baacb9714cc16b5b82"
                };
                const getPumpNoListPromise = getPumpNoList(getPumpNoListParams);
                const getUserListPromise = useruser_list(getUserListParams);
                const getPumpNoListRes = await getPumpNoListPromise;
                const getUserListRes = await getUserListPromise
                if(getPumpNoListRes && getPumpNoListRes.code === 1001) {
                    this.pumpNoOptions = [...getPumpNoListRes.data.map(item=> ({value: item.code, label: item.carNo}))]
                }
                if(getUserListRes && getUserListRes.code === 1001) {
                    this.pumpManOptions = [...getUserListRes.data.map(item=> ({value: item.userId, label: item.userName}))]
                }
            },
            onClose() {
                this.$emit("update:visible", false)
            },
            async onConfirm() {
                const params = {
                    id: this.config.applyDetId,
                    ...this.form
                }
                const res = await updateApplyDet(params)
                if(res && res.code === 1001) {
                    this.$message.success("保存成功")
                    this.$emit("updated",false)
                    this.$emit("update:visible", false)
                } else {
                    this.$message.warning("保存失败")
                }
                
            },
        },
    };
</script>
<style lang="scss" scoped></style>